var axios = require("axios");

var config = {
  method: "get",
  url: "https://fanyi.baidu.com/pcnewcollection?req=get&dstStatus=all&order=time&direction=all",
  headers: {
    Connection: "keep-alive",
    Pragma: "no-cache",
    "Cache-Control": "no-cache",
    "sec-ch-ua":
      '" Not A;Brand";v="99", "Chromium";v="96", "Microsoft Edge";v="96"',
    Accept: "application/json, text/javascript, */*; q=0.01",
    "X-Requested-With": "XMLHttpRequest",
    "sec-ch-ua-mobile": "?0",
    "User-Agent":
      "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36 Edg/96.0.1054.62",
    "sec-ch-ua-platform": '"macOS"',
    "Sec-Fetch-Site": "same-origin",
    "Sec-Fetch-Mode": "cors",
    "Sec-Fetch-Dest": "empty",
    Referer: "https://fanyi.baidu.com/collection",
    "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
    Cookie:
      "BIDUPSID=BEA088E1FF6B48BF8927CCE87EE126E2; PSTM=1639190442; __yjs_duid=1_23d39d30e86b3ed3f62d4be55f8416a21639201272359; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; BAIDUID=BE3786DEA587C634FDFE8785E4230708:FG=1; BDSFRCVID_BFESS=59tOJeC62uwnsxvHRaBVUOkKb27ZcbJTH6aovkeKl3lr8lD9Q3q5EG0P8f8g0Kub01XPogKK0eOTHktF_2uxOjjg8UtVJeC6EG0Ptf8g0f5; H_BDCLCKID_SF_BFESS=tJC8oD0KtD03eb5m-trSqR0D-fvQetJyaR0tV4bvWJ5TMCoG5PT52bKn0xQGLqQ9223aoD3uKbokShPC-tn-MPLUhabQbjvQ3n5Io-Da3l02VKnIe-t2ynLVDaQvQPRMW20j0l7mWnvMsxA45J7cM4IseboJLfT-0bc4KKJxbnLWeIJIjj6jK4JKDH8Ot53P; BAIDUID_BFESS=FFFCBF09D684ABAD100C3C4201ADD0FD:FG=1; APPGUIDE_10_0_2=1; BAIDU_WISE_UID=wapp_1640753288502_872; BDUSS=QtLUk1U3NaOGpNZVE5NVlHZ2hsVzdjZlFWTjdmdGFDZS14OE03eX5nbk5tUFpoSVFBQUFBJCQAAAAAAAAAAAEAAABXlgOiQWNxdWG98MuuAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAM0Lz2HNC89hTz; BDUSS_BFESS=QtLUk1U3NaOGpNZVE5NVlHZ2hsVzdjZlFWTjdmdGFDZS14OE03eX5nbk5tUFpoSVFBQUFBJCQAAAAAAAAAAAEAAABXlgOiQWNxdWG98MuuAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAM0Lz2HNC89hTz; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; delPer=0; PSINO=5; H_PS_PSSID=35635_35105_31660_34584_35491_35689_35645_35319_26350_35475_35558; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1639567517,1639793955,1640741097,1641278056; __yjs_st=2_YWZhYmNmYjNkZDAxNTRlNGQzNzJhZjQ3MGM2MTAyMzFmZTNjZGJhYTFjNWFkNzZkNThiYjFlMDUyM2YxOTQ1NThlM2FiYzI2ZTgxOGM5NDM4OTQ2Mzc2NjhkNDhiNmQyOTU5OWVlNTAyZWJlZWNiOTA1MWVhNDk3ZjY2MzgyODkyZDUxMmUwOTdkNDcxZWZhZDExNjgyYzllMDMyMDkyZGUyN2Q0OTYzODU1YTgzODYwNDQ0YTA0NTMyNGI2OTAwMzBkMzBlYjlmNDQ1ZjAyZTM1YmJkOGI5MzUwMzUzNGE0NTk4MzM4ZDM4MTU1MDFkODI5NmQ5Mzc1NmUyYTk3Yl83XzBhYzM4ZjI4; ab_sr=1.0.1_Nzc0YzI2NDNhMjEwNTc5MTY3ODIwMGQ0NDVlYjBiY2JlYTM4ZTM3NzI0NWM1Y2Y1NDhkMjllNzNjMmU4ODllNTBhNmE3YjBjNGJlZjBhZmMzOWZiMzNlZmJjZmExMDIwMTM2YmEwMTQ2ODY5Y2M1NTE5ZjMwYzcxODE5Mzc1OWY4ODliNGJjMDJmMDM3ZTM4NDI0ZmU2YjQ2YzVlNzVjNDE2MmYzNDMxOTExNmNmZTJkNmM4NjkzNmZmNTk2MTIy; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1641515285",
  },
};

const makeUrl = (argObj) => {
  return Object.entries(argObj)
    .map((item) => "&" + item.join("="))
    .join("");
};

const dict2means = (dict) => {
  if (dict.includes("\r\n")) {
    return dict
      .split("\r\n")
      .slice(1)
      .map((item) => item.replace(/\s/g, ""));
  } else {
    return dict.split("\n");
  }
};

// const parseWordItem = (rawData) => {
//   const {
//     dictjson,
//     dict: word_means,
//     updatetime,
//   } = JSON.parse(JSON.stringify(rawData));
//   const wordData = JSON.parse(dictjson).simple_means;
//   const { word_name } = wordData;
//   return { word_name, word_means, updatetime };
// };

const parseWordItem = (rawData) => {
  const {
    fanyisrc: word_name,
    dict,
    fanyidst,
    updatetime,
  } = JSON.parse(JSON.stringify(rawData));
  const word_means = dict2means(dict) || fanyidst;
  return { word_name, word_means, updatetime };
};

const getCollectionPage = async (page = 1, pagesize = 30) => {
  const response = await axios({
    ...config,
    url: config.url + makeUrl({ page, pagesize }),
  });
  const { currentpage, total, totalpage, pageinfo: wordList } = response.data;
  const res = {
    currentpage,
    total,
    totalpage,
    wordList: wordList.map(parseWordItem),
  };
  return res;
};

module.exports = {
  getCollectionPage,
};
